package com.yk.satoken.demos.config;

import com.yk.satoken.demos.minio.config.MinioConfig;
import com.yk.satoken.demos.minio.util.UpUtil;
import io.minio.*;
import javafx.scene.input.DataFormat;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.datetime.DateFormatter;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import org.springframework.web.multipart.MultipartFile;

import java.io.*;
import java.nio.file.Paths;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.UUID;

/**
 * @author YangKai
 * @create 2024-06-02 16:08
 * @version: 1.0
 * @Description:
 */
@Service
public class MinioStorageServiceImpl {

    @Autowired
    private MinioConfig minioConfig;

    @Autowired
    private MinioClient minioClient;

    public InputStream getObject(String fileName) {
        try {
            return minioClient.getObject(
                    GetObjectArgs.builder()
                            .bucket(this.minioConfig.getBucketName())
                            .object(fileName)
                            .build());
        } catch (Exception e) {
            e.printStackTrace();
            throw new RuntimeException("minio获取文件失败:" + fileName);
        }
    }

    public String builderFilePath(String filename) {
        StringBuilder stringBuilder = new StringBuilder(50);

        SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd");
        String todayStr = sdf.format(new Date());
        stringBuilder.append(todayStr).append(",");
        stringBuilder.append(filename);
        return stringBuilder.toString();
    }


    public void uploadFile(MultipartFile file, String fileName) {
        try {
            // 设置要上传到的MinIO存储桶的名称，从minioConfig对象中获取
            PutObjectArgs putObjectArgs = PutObjectArgs.builder().bucket(this.minioConfig.getBucketName())
                    // 置要上传的文件在MinIO存储桶中的对象名称，即文件名
                    .object(fileName)
                    // 设置要上传的文件的输入流、文件大小和分片大小。这里使用-1表示不进行分片上传。
                    .stream(file.getInputStream(), file.getSize(), -1)
                    // 设置要上传的文件的内容类型，从file对象中获取
                    .contentType(file.getContentType())
                    .build();
            // 调用MinIO客户端的putObject方法，用于将文件上传到MinIO存储桶中。
            minioClient.putObject(putObjectArgs);
        } catch (Exception e) {
            e.printStackTrace();
            throw new RuntimeException("minio上传文件失败");
        }
    }


    public void uploadFile(InputStream inputStream, String contentType, String fileName) {
        try {
            minioClient.putObject(
                    PutObjectArgs.builder().bucket(this.minioConfig.getBucketName())
                            .object(fileName).stream(
                                    inputStream, -1, 10485760)
                            .contentType(contentType)
                            .build());
        } catch (Exception e) {
            e.printStackTrace();
            throw new RuntimeException("minio上传文件失败");
        }
    }


    // 创建桶
    private Boolean createBucketName() {
        Boolean flag = Boolean.TRUE;
        try {
            boolean exists = minioClient.bucketExists(BucketExistsArgs.builder().build());
            if (exists) {
                System.out.println("Bucket already exists.");
            } else {
                // 如果不存在，则创建一个新的存储桶
                minioClient.makeBucket(MakeBucketArgs.builder().build());
                System.out.println("Bucket created successfully.");
            }
        } catch (Exception e) {
            e.getMessage();
            return Boolean.FALSE;
        }
        return flag;
    }


    public InputStream downloadFileByPath(String path) {
        try {
            return minioClient.getObject(
                    GetObjectArgs.builder()
                            .bucket(this.minioConfig.getBucketName())
                            .object(path)
                            .build());
        } catch (Exception e) {
            e.printStackTrace();
            throw new RuntimeException("minio获取文件失败:" + path);
        }
    }

    public String getUrl() {
        try {
            return "http://192.168.200.166:9000/file/2024/06/02/5bd2fc18efae4ef8b8593a5bb8059d6e.jpg";
        } catch (Exception e) {
            e.printStackTrace();
            throw new RuntimeException("minio获取文件失败:");
        }

    }
}
